package com.tonghu.bluetoothregistration.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tonghu.bluetoothregistration.R;
import com.tonghu.bluetoothregistration.bean.ScanData;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by tonghu on 6/7/15.
 */
public class ListDeviceFragment extends android.support.v4.app.ListFragment{
    private static final int MAX_NUM = 1000;
    private Map<String, ScanData> dataMap = new HashMap<>();
    private List<ScanData> datas = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setListAdapter(curAdapter);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
//        setEmptyText("等待扫描中");
    }

    @Override
    public void onDestroyOptionsMenu() {
        super.onDestroyOptionsMenu();
        EventBus.getDefault().unregister(this);
    }

    private BaseAdapter curAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return datas.size();
//            return dataMap.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            // General ListView optimization code.
            if (view == null) {
                LayoutInflater from = LayoutInflater.from(viewGroup.getContext());
                view = from.inflate(R.layout.listitem_device_f, viewGroup, false);
                viewHolder = new ViewHolder();
                viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
                viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
                viewHolder.deviceUUID= (TextView)view.findViewById(R.id.device_beacon_uuid);
                viewHolder.deviceMajor_Minor=(TextView)view.findViewById(R.id.device_major_minor);
                viewHolder.devicetxPower_RSSI=(TextView)view.findViewById(R.id.device_txPower_rssi);
                viewHolder.devicePos_x_y=(TextView)view.findViewById(R.id.devicePos_x_y);
                viewHolder.deviceDistance=(TextView)view.findViewById(R.id.deviceDistance);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }

            ScanData device = datas.get(i);
            final String deviceName = device.name;
            //double distance = device.distance;
            DecimalFormat df = new DecimalFormat("#.00");
            if (deviceName != null && deviceName.length() > 0)
                viewHolder.deviceName.setText(deviceName);
            else
                viewHolder.deviceName.setText("unknow device");

            viewHolder.deviceAddress.setText(device.bluetoothAddress);
            viewHolder.deviceUUID.setText(device.proximityUuid);
            viewHolder.deviceMajor_Minor.setText("major:"+device.major+",minor:"+device.minor);
            //viewHolder.devicetxPower_RSSI.setText("txPower:"+device.txPower+",rssi:"+ df.format(iBeaconClass.averageRssi[device.major]));
            viewHolder.devicetxPower_RSSI.setText("txPower:"+device.txPower+",rssi:"+ df.format(device.rssi));

//		if(iBeaconClass.majorDistance[device.major] < 1.0)
//			viewHolder.deviceDistance.setText("Distance : 0"+df.format(iBeaconClass.majorDistance[device.major]));
//		else
//			viewHolder.deviceDistance.setText("Distance : "+df.format(iBeaconClass.majorDistance[device.major]));
            viewHolder.deviceDistance.setText("Distance : 0");
            viewHolder.devicePos_x_y.setText("Pos_radius:");
            return view;
        }

        class ViewHolder {
            TextView deviceName;
            TextView deviceAddress;
            TextView deviceUUID;
            TextView deviceMajor_Minor;
            TextView devicetxPower_RSSI;
            TextView deviceDistance;
            TextView devicePos_x_y;
        }
    };

    public void onEvent(Object obj) {
        if (obj instanceof ScanData) {
            for(int i=0;i<datas.size();i++){
                String btAddress = datas.get(i).bluetoothAddress;
                if(btAddress.equals(((ScanData)obj).bluetoothAddress)){
                    datas.add(i + 1, (ScanData) obj);
                    datas.remove(i);
                    curAdapter.notifyDataSetChanged();
                    return;
                }
            }
            datas.add((ScanData) obj);
            curAdapter.notifyDataSetChanged();
        }
    }


}
